package com.lee.dhsg.dao;

import com.lee.dhsg.dao.provider.CourseSqlProvider;
import com.lee.dhsg.domain.Course;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author Jun
 */
@Component
@Mapper
public interface CourseDao {

    /**
     * 通过课程名模糊查询课程
     *
     * @author Lee
     */
    @Select("select * from course WHERE cname LIKE CONCAT ('%',#{courseName},'%')")
    List<Course> selectByName(String courseName);

    /**
     * 根据id查找课题
     * @param courseid
     * @return Course
     */
    @Select("select * from course where courseid=#{courseid}")
    Course selectById(Integer courseid);

    /**
     * 根据分类id查找课题
     * @param categoryid
     * @return List<Course>
     */
    @Select("select * from course where categoryid=#{categoryid}")
    List<Course> selectBycategoryid(Integer categoryid);

    /**
     * 根据教师id查课题
     * @param tid
     * @return List<Course>
     */
    @Select("select * from course where tid=#{tid}")
    @Results({
            @Result(id=true,column = "courseid",property = "courseid"),
            @Result(column = "cname",property = "cname"),
            @Result(column = "img",property = "img"),
            @Result(column = "originaPrice",property = "originaPrice"),
            @Result(column = "discountPrice",property = "discountPrice"),
            @Result(column = "discount",property = "discount"),
            @Result(column = "reducePrice",property = "reducePrice"),
            @Result(column = "duration",property = "duration"),
            @Result(column = "onlineTime",property = "onlineTime"),
            @Result(column = "courseDescriptin",property = "courseDescriptin"),
            @Result(column = "resource",property = "resource"),
            @Result(column = "discountDescription",property = "discountDescription"),
            @Result(column = "discountBeginTime",property = "discountBeginTime"),
            @Result(column = "discountStopTime",property = "discountStopTime"),
            @Result(column = "discountStatus",property = "discountStatus"),
            @Result(column = "tid",property = "teacher",
                    one=@One(
                            select = "com.dhsg.dao.TeacherDao.selectByTid",
                            fetchType = FetchType.EAGER
                    ))
    })
    List<Course> seletCourseByTid(Integer tid);

    /**
     * 根据id删除课题
     * @param courseid
     */
    @Select("delete from course where courseid=#{courseid}")
    void deleteByCourseid(Integer courseid);


    /**
     * 动态查询课程的数量
     * @param params
     * @return Integer
     */
    @SelectProvider(type = CourseSqlProvider.class,method = "count")
    Integer count(Map<String, Object> params);

    /**
     * 根据动态参数查询课题
     * @param params
     * @return List<Course>
     */
    @SelectProvider(type= CourseSqlProvider.class,method = "selectWhitParam")
    @Results({
            @Result(id=true,column = "courseid",property = "courseid"),
            @Result(column = "cname",property = "cname"),
            @Result(column = "img",property = "img"),
            @Result(column = "originaPrice",property = "originaPrice"),
            @Result(column = "discountPrice",property = "discountPrice"),
            @Result(column = "discount",property = "discount"),
            @Result(column = "reducePrice",property = "reducePrice"),
            @Result(column = "duration",property = "duration"),
            @Result(column = "onlineTime",property = "onlineTime"),
            @Result(column = "courseDescriptin",property = "courseDescriptin"),
            @Result(column = "resource",property = "resource"),
            @Result(column = "discountDescription",property = "discountDescription"),
            @Result(column = "discountBeginTime",property = "discountBeginTime"),
            @Result(column = "discountStopTime",property = "discountStopTime"),
            @Result(column = "discountStatus",property = "discountStatus"),
            @Result(column = "realname",property = "teacher",
                    one=@One(
                            select = "com.dhsg.dao.TeacherDao.selectByPage",
                            fetchType = FetchType.EAGER
                    )),
            @Result(column = "name",property = "category",
                    one=@One(
                            select = "com.dhsg.dao.CategoryDao.selectOneByName",
                            fetchType = FetchType.EAGER
                    ))
    })
    List<Course> selectByPage(Map<String, Object> params);

    /**
     * 动态插入课题
     * @param course
     */
    @SelectProvider(type= CourseSqlProvider.class,method ="insertCourse")
    @Options(useGeneratedKeys = true,keyProperty = "courseid")
    void saveCourse(Course course);

    /**
     *动态更新课题
     * @param course
     */
    @SelectProvider(type= CourseSqlProvider.class,method = "updateCourse")
    void updateCourse(Course course);

}
